package com.Adapter;

import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.bolao.R;
import com.entidades.Match;


public class MatchAdapter extends BaseAdapter {

	 private Context mContext;

	    List<Match>data;
	    private LayoutInflater mInflater;
	    private static ArrayAdapter<CharSequence> goalScoreAdapter;
	    private boolean isEditable;
	
	   public MatchAdapter(Context mContext, List<Match> data, Boolean isEditable){
	        this.data = data;
	        this.mContext = mContext; 
	        mInflater = LayoutInflater.from(mContext);
	        this.isEditable = isEditable;
	        if(goalScoreAdapter==null)
	    	goalScoreAdapter = ArrayAdapter.createFromResource(mContext, R.array.goalScore, android.R.layout.simple_spinner_item);  
	        
	   }
	   
		static class ViewHolder{
			private ImageView imgTeamA;
			private TextView teamNameA;
			private TextView teamNameB;
			private TextView date;
			private TextView dateTime;
			private ImageView imgTeamB;
			private Spinner spinnerA;
			private Spinner spinnerB;
			private TextView scoreA;
			private TextView scoreB;
		}
	    
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		return data.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return data.get(arg0).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.match_layout, null);
			holder = new ViewHolder();  
			holder.imgTeamA = (ImageView) convertView.findViewById(R.id.teamFlagA);
            holder.teamNameA = (TextView) convertView.findViewById(R.id.teamNameA);
            holder.imgTeamB = (ImageView) convertView.findViewById(R.id.teamFlagB);
            holder.teamNameB = (TextView) convertView.findViewById(R.id.teamNameB);
            holder.spinnerA = (Spinner) convertView.findViewById(R.id.editTextResultTeamA);
            holder.spinnerB = (Spinner) convertView.findViewById(R.id.editTextResultTeamB);
            holder.date = (TextView) convertView.findViewById(R.id.textViewDate);
            holder.dateTime  = (TextView) convertView.findViewById(R.id.textViewDateTime);
            holder.scoreA = (TextView) convertView.findViewById(R.id.TextViewResultTeamA);
            holder.scoreB = (TextView) convertView.findViewById(R.id.TextViewResultTeamB);
            
            if (isEditable){
            	holder.scoreA.setVisibility(View.INVISIBLE);
            	holder.scoreB.setVisibility(View.INVISIBLE);
            } else {
                holder.spinnerA.setVisibility(View.INVISIBLE);
                holder.spinnerB.setVisibility(View.INVISIBLE);
            }
            convertView.setTag(holder);
                  
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Resources r = mContext.getResources();		
		Match match = data.get(position);
		holder.imgTeamA.setImageResource(r.getIdentifier(match.getTeamA().getFlag(), "drawable",  mContext.getPackageName()));
		holder.teamNameA.setText(match.getTeamA().getLocalizedName());
		holder.imgTeamB.setImageResource(r.getIdentifier(match.getTeamB().getFlag(), "drawable",  mContext.getPackageName()));
		holder.teamNameB.setText(match.getTeamB().getLocalizedName());

		@SuppressWarnings("deprecation")
		String date = match.getDate().getDate() + "/" +  match.getDate().getMonth();
		holder.date.setText(date);
		@SuppressWarnings("deprecation")
		String time = match.getDate().getHours() + ":" +  match.getDate().getMinutes() + "0";
		holder.spinnerA.setAdapter(goalScoreAdapter);
		holder.spinnerB.setAdapter(goalScoreAdapter);
		holder.dateTime.setText(time);
		if(isEditable){
		holder.spinnerA.setOnItemSelectedListener(new GetMatchResultBetListener(match, true));
		holder.spinnerB.setOnItemSelectedListener(new GetMatchResultBetListener(match, false));
		
		if(match.hasUserAnswer()){
			holder.spinnerA.setSelection(match.getScoreUserBetTA()+1);
			holder.spinnerB.setSelection(match.getScoreUserBetTB()+1);
		}	
		} else if(match.hasScore()){
			holder.scoreA.setText(String.valueOf(match.getScoreTA()));
			holder.scoreB.setText(String.valueOf(match.getScoreTB()));

		} 
 
		return convertView;
	}
	
	public class GetMatchResultBetListener implements OnItemSelectedListener {
		 
		private Match match;
		private boolean isTeamA;
		public GetMatchResultBetListener(Match match, boolean isTeamA){
			super();
			this.match = match;
			this.isTeamA = isTeamA;
		}
		@Override
	  public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
		  if(!parent.getItemAtPosition(pos).equals("")){
			  if(isTeamA){
				  match.setScoreUserBetTA(Integer.valueOf((String) parent.getItemAtPosition(pos)));
			  } else {
				  match.setScoreUserBetTB(Integer.valueOf((String) parent.getItemAtPosition(pos)));
		  	}
		  } else {
			  if(isTeamA){
				  match.setScoreUserBetTA(-1);
			  } else {
				  match.setScoreUserBetTB(-1);
		  	}
		  }

	  }
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {

		
	}
	 
	 
	}

}
